let fs = require('fs');

class Events {
constructor(){
  this.stack =[];
}
on(callback){
  this.stack.push(callback);
}
emit(){
  this.stack.forEach(c=>c());
}
}
let events = new Events();
let school = {};
events.on(function(){
  if(Object.keys(school).length===2){
    console.log(school);
  }
})
events.on(function(){
  console.log('每次都执行');
})
//订阅发布
//希望两次完成 后打印最终结果  1 对 多的事务
fs.readFile('./name.txt', 'utf-8', function (err, data) {
  school.name = data;
  events.emit();
})
fs.readFile('./age.txt', 'utf-8', function (err, data) {
  school.age = data;
  events.emit();
})
